$(function () {

    var dic_datagrid = $("#dic_datagrid");
    var dicItem_datagrid = $("#dicItem_datagrid");
    var dic_dialog = $("#dic_dialog");
    var dicItem_dialog = $("#dicItem_dialog");
    var dic_form = $("#dic_form");
    var dicItem_form = $("#dicItem_form");
    var dicid;

    dic_datagrid.datagrid({
        fitColumns:true,
        fit:true,
        toolbar: '#dic_toolbar',
        url: '/dic/list.do',
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '目录编号', field: 'sn', width: '100'},
            {title: '目录名称', field: 'name', width: '100'},
            {title: '目录简介', field: 'intro', width: '100'},
            {title: '状态', field: 'state', width: '100', formatter: function (value, row, index) {
                    return value ? "<font color='green'>启用</font>" : "<font color='red'>停用</font>"
                }}
        ]],
        onClickRow:function (index, row) {
          //  var row = dic_datagrid.datagrid("getSelected");
            dicid=row.id;
            dicItem_datagrid.datagrid("load",{"dicid": dicid});
        }
    })


    dic_dialog.dialog({
        width: 250,
        height: 250,
        buttons: '#dic_buttons',
        closed: true,
        onClose: function () {
            dic_form.form("clear")
        }

    })
    dicItem_datagrid.datagrid({
        fitColumns:true,
        fit:true,
        toolbar: '#dicItem_toolbar',
        url: '/dic/listitem.do',
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '明细名称', field: 'name', width: '100'},
            {title: '明细简介', field: 'intro', width: '100'},
            {title: '所属目录', field: 'dic', width: '100',formatter: function (value, row, index) {
                    return row.dic ? row.dic.name : "";
                }
            },
            {title: '状态', field: 'state', width: '100', formatter: function (value, row, index) {
                    return value ? "<font color='green'>启用</font>" : "<font color='red'>停用</font>"
                }}
        ]]
    })


    dicItem_dialog.dialog({
        width: 250,
        height: 250,
        buttons: '#dicItem_buttons',
        closed: true,
        onClose: function () {
            dicItem_form.form("clear")
        }
    })




    var methodObj = {
        //添加按钮
        add: function () {
            dic_dialog.dialog("open");
            dic_dialog.dialog('setTitle', '新增目录');
        },
        //添加按钮
        addItem: function () {
            console.log(dicid);
            $("#dic_id").prop("value",dicid);
            dicItem_dialog.dialog("open");
            dicItem_dialog.dialog('setTitle', '新增明细');
        },
        //编辑按钮
        edit: function () {
            var data = dic_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }

            dic_form.form('load', data);

            dic_dialog.dialog("open");
            dic_dialog.dialog('setTitle', '编辑目录');
        },
        //编辑按钮
        editItem: function () {
            $("#dic_id").prop("value",dicid);
            var data = dicItem_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            dicItem_form.form('load', data);
            dicItem_dialog.dialog("open");
            dicItem_dialog.dialog('setTitle', '编辑明细');
        },
        //启用/停用
        change: function () {
            var row = dic_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要变更的目录信息", "info");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要变更该目录吗？", function (r) {
                if (r) {
                    $.get("/dic/change.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "变更成功", "info", function () {
                                dic_datagrid.datagrid("reload");
                                dicItem_datagrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    });
                }
            })
        },
        //启用/停用
        changeItem: function () {
            var row = dicItem_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要变更的明细信息", "info");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要变更该明细吗？", function (r) {
                if (r) {
                    $.get("/dic/changeitem.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "变更成功", "info", function () {
                                dicItem_datagrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    });
                }
            })
        },
        //刷新按钮
        reload: function () {
            dic_datagrid.datagrid("reload");
        },
        //刷新按钮
        reloadItem: function () {
            dicItem_datagrid.datagrid("reload");
        },
        //取消按钮
        cancel: function () {
            dic_dialog.dialog("close");
        },
        //取消按钮
        cancelItem: function () {
            dicItem_dialog.dialog("close");
            //dicItem_datagrid.datagrid("reload");
        },
        //添加保存按钮
        save: function () {
            dic_form.form('submit', {
                url: '/dic/saveDic.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancel();
                            dic_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                }
            })
        },
        //添加保存按钮
        saveItem: function () {
            dicItem_form.form('submit', {
                url: '/dic/saveDicitem.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancelItem();
                            dicItem_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                }
            })
        },
    }

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    })

})

